//
//  LoseCodeView.m
//  SameWorld(merchant)
//
//  Created by 李小朋 on 2017/3/31.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "LoseCodeView.h"

@interface LoseCodeView ()
@property (nonatomic ,strong) NSArray *imageArr;
@property (nonatomic ,strong) NSArray *selectImageArr;
@property (nonatomic ,assign) NSInteger numberZero;
@end


@implementation LoseCodeView

- (NSArray *)imageArr {
    if (!_imageArr) {
        _imageArr = @[@"ic_1budianji",@"ic_2budianji",@"ic_3budianji",@"ic_4budianji",@"ic_5budianji",@"ic_6budianji",@"ic_7budianji",@"ic_8budianji",@"ic_9budianji",@"ic_0budianji"];
    }
    return _imageArr;
}

- (NSArray *)selectImageArr {
    
    if (!_selectImageArr) {
        _selectImageArr = @[@"ic_1anxia",@"ic_2dianji",@"ic_3dianji",@"ic_4dianji",@"ic_5dianji",@"ic_6dianji",@"ic_7dianji",@"ic_8dianji",@"ic_9dianji",@"ic_0dianji"];
    }
    return _selectImageArr;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatBtn];
    }
    return self;
}



- (void)creatBtn {
    
    int totalloc = 3;
    CGFloat btnw = 68;
    CGFloat btnh = 68;
    
//    CGFloat margin = (self.frame.size.width - totalloc*btnw)/(totalloc + 1);
    CGFloat margin = 12;
   
    for (int i = 0; i < self.imageArr.count; i ++) {
        int row = i/totalloc;
        int loc = i%totalloc;
        
        CGFloat btnx = margin + (margin + btnw)*loc;
        CGFloat btny = margin + (margin + btnh)*row;
        
        UIButton *keyboardBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        keyboardBtn.frame = CGRectMake(btnx, btny, btnw, btnh);
        [keyboardBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",self.imageArr[i]]] forState:(UIControlStateNormal)];
        [keyboardBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",self.selectImageArr[i]]] forState:(UIControlStateHighlighted)];
        keyboardBtn.tag = i + 300;
        [keyboardBtn addTarget:self action:@selector(keyboardBtnClicked:) forControlEvents:(UIControlEventTouchUpInside)];
        [self addSubview:keyboardBtn];
        
        // 默认不可点击,当编辑的时候打开
        keyboardBtn.enabled = NO;
        if (i == 0) {
            self.keyboardBtn1 = keyboardBtn;
        }
        if (i == 1) {
            self.keyboardBtn2 = keyboardBtn;
        }
        if (i == 2) {
            self.keyboardBtn3 = keyboardBtn;
        }
        if (i == 3) {
            self.keyboardBtn4 = keyboardBtn;
        }
        if (i == 4) {
            self.keyboardBtn5 = keyboardBtn;
        }
        if (i == 5) {
            self.keyboardBtn6 = keyboardBtn;
        }
        if (i == 6) {
            self.keyboardBtn7 = keyboardBtn;
        }
        if (i == 7) {
            self.keyboardBtn8 = keyboardBtn;
        }
        if (i == 8) {
            self.keyboardBtn9 = keyboardBtn;
        }
        if (i == 9) {
            self.keyboardBtn0 = keyboardBtn;
        }
    }
    UIButton *checkCodeBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    checkCodeBtn.frame = CGRectMake(btnw+margin*2,3*btnh + 4*margin, 148, 68);
    [checkCodeBtn setImage:[UIImage imageNamed:@"ic_yanzheng_01"] forState:(UIControlStateNormal)];
    [checkCodeBtn addTarget:self action:@selector(checkCodeBtnClicked:) forControlEvents:(UIControlEventTouchUpInside)];
    [self addSubview:checkCodeBtn];
   
}

#pragma mark - 数字按钮
- (void)keyboardBtnClicked:(UIButton *)btn {
    
    // 判断是否为0
    if (btn.tag == 309) {
        NSInteger temp = self.keyboardBtn0.tag - 309;
        self.buttonNumberClicked(temp);
    }else {
        NSInteger index =(NSInteger)btn.tag -300 +1;
        self.buttonNumberClicked(index);
    }
    
}


#pragma mark - 验码按钮
- (void)checkCodeBtnClicked:(UIButton *)btn {

    NSLog(@"验码");
    if (self.delegate && [self.delegate respondsToSelector:@selector(checkCodeButton)]) {
        [self.delegate checkCodeButton];
    }
    
}



@end
